package gui;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ItemEvent;
import java.rmi.RemoteException;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JOptionPane;

import client.ClientImpl;

public class ClientFrame extends javax.swing.JFrame {

	private static final long serialVersionUID = 1L;
	protected String brokerManagerURI;
	private BrokerShutdown bs = new BrokerShutdown(this, false);
	private javax.swing.JCheckBoxMenuItem checkBoxMenuItemDistributionVersion;
	protected ClientImpl client;
	private final int FRAME_HEIGHT_DISTRIBUTION = 565;
	private final int FRAME_HEIGHT_TEST = 640;
	private final int FRAME_WIDTH = 730;
	private javax.swing.JMenuBar menuBar;
	private javax.swing.JMenu menuSettings;
	private gui.PanelIP panelIP;
	private gui.PanelMessagesLog panelMessagesLog;
	private gui.PanelMultiPublish panelMultiPublish;
	private gui.PanelNameAndButtons panelNameAndButtons;
	private gui.PanelPublish panelPublish;
	private gui.PanelTopicManagement panelTopicManagement;

	public ClientFrame() {
		initComponents();
		if (!initializeBrokerManagerURI())
			this.dispose();
		setCenterLocation();
		try {
			client = new ClientImpl(this);
		} catch (RemoteException e) {
			e.printStackTrace();
		}
	}

	public void brokerShutdownProgress(int progress) {
		bs.setProgress(progress);
		if (isEnabled())
			setEnabled(false);
		if (!bs.isVisible())
			bs.setVisible(true);
	}

	private void checkBoxMenuItemDistributionVersionItemStateChanged(
			java.awt.event.ItemEvent evt) {
		if (evt.getStateChange() == ItemEvent.SELECTED) {
			hideComponents(true);
			this.setSize(FRAME_WIDTH, FRAME_HEIGHT_DISTRIBUTION);
		}
		if (evt.getStateChange() == ItemEvent.DESELECTED) {
			hideComponents(false);
			this.setSize(FRAME_WIDTH, FRAME_HEIGHT_TEST);
		}
	}

	private void formWindowClosing(java.awt.event.WindowEvent evt) {
		try {
			ClientGuiLauncher.clientFrame.client.stop(true);
		} catch (Exception e) {
			Logger.getLogger(ClientFrame.class.getName()).log(Level.SEVERE,
					null, e);
		}
	}

	public PanelIP getPanelIP() {
		return panelIP;
	}

	public PanelMessagesLog getPanelMessagesLog() {
		return panelMessagesLog;
	}

	public PanelMultiPublish getPanelMultiPublish() {
		return panelMultiPublish;
	}

	public PanelNameAndButtons getPanelNameAndButtons() {
		return panelNameAndButtons;
	}

	public PanelPublish getPanelPublish() {
		return panelPublish;
	}

	public PanelTopicManagement getPanelTopicManagement() {
		return panelTopicManagement;
	}

	private void hideComponents(boolean hide) {
		panelMultiPublish.setVisible(!hide);
		panelNameAndButtons.getButtonReconnect().setVisible(!hide);
		panelNameAndButtons.getButtonStop().setVisible(!hide);
	}

	private void initComponents() {

		panelIP = new gui.PanelIP();
		panelNameAndButtons = new gui.PanelNameAndButtons();
		panelTopicManagement = new gui.PanelTopicManagement();
		panelMessagesLog = new gui.PanelMessagesLog();
		panelPublish = new gui.PanelPublish();
		panelMultiPublish = new gui.PanelMultiPublish();
		menuBar = new javax.swing.JMenuBar();
		menuSettings = new javax.swing.JMenu();
		checkBoxMenuItemDistributionVersion = new javax.swing.JCheckBoxMenuItem();

		setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
		setResizable(false);
		addWindowListener(new java.awt.event.WindowAdapter() {
			@Override
			public void windowClosing(java.awt.event.WindowEvent evt) {
				formWindowClosing(evt);
			}
		});

		panelTopicManagement.setBorder(javax.swing.BorderFactory
				.createEtchedBorder());

		panelMessagesLog.setBorder(javax.swing.BorderFactory
				.createEtchedBorder());

		panelPublish.setBorder(javax.swing.BorderFactory.createEtchedBorder());

		panelMultiPublish.setBorder(javax.swing.BorderFactory
				.createEtchedBorder());

		menuSettings.setText("Settings");

		checkBoxMenuItemDistributionVersion.setText("Distribution version");
		checkBoxMenuItemDistributionVersion
				.addItemListener(new java.awt.event.ItemListener() {
					@Override
					public void itemStateChanged(java.awt.event.ItemEvent evt) {
						checkBoxMenuItemDistributionVersionItemStateChanged(evt);
					}
				});
		menuSettings.add(checkBoxMenuItemDistributionVersion);

		menuBar.add(menuSettings);

		setJMenuBar(menuBar);

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap()
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.LEADING)
												.addComponent(panelPublish, 0,
														0, Short.MAX_VALUE)
												.addGroup(
														layout.createSequentialGroup()
																.addGroup(
																		layout.createParallelGroup(
																				javax.swing.GroupLayout.Alignment.CENTER)
																				.addComponent(
																						panelIP,
																						javax.swing.GroupLayout.PREFERRED_SIZE,
																						277,
																						javax.swing.GroupLayout.PREFERRED_SIZE)
																				.addComponent(
																						panelTopicManagement,
																						javax.swing.GroupLayout.PREFERRED_SIZE,
																						javax.swing.GroupLayout.DEFAULT_SIZE,
																						javax.swing.GroupLayout.PREFERRED_SIZE))
																.addPreferredGap(
																		javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																.addGroup(
																		layout.createParallelGroup(
																				javax.swing.GroupLayout.Alignment.CENTER)
																				.addComponent(
																						panelNameAndButtons,
																						javax.swing.GroupLayout.PREFERRED_SIZE,
																						javax.swing.GroupLayout.DEFAULT_SIZE,
																						javax.swing.GroupLayout.PREFERRED_SIZE)
																				.addComponent(
																						panelMessagesLog,
																						javax.swing.GroupLayout.PREFERRED_SIZE,
																						javax.swing.GroupLayout.DEFAULT_SIZE,
																						javax.swing.GroupLayout.PREFERRED_SIZE)))
												.addComponent(
														panelMultiPublish,
														javax.swing.GroupLayout.Alignment.TRAILING,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														700, Short.MAX_VALUE))
								.addContainerGap()));
		layout.setVerticalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap()
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.LEADING)
												.addComponent(
														panelIP,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE)
												.addComponent(
														panelNameAndButtons,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE))
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.LEADING,
												false)
												.addComponent(
														panelMessagesLog,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														Short.MAX_VALUE)
												.addComponent(
														panelTopicManagement,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														Short.MAX_VALUE))
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(panelPublish,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										javax.swing.GroupLayout.DEFAULT_SIZE,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(panelMultiPublish,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										javax.swing.GroupLayout.DEFAULT_SIZE,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addContainerGap(
										javax.swing.GroupLayout.DEFAULT_SIZE,
										Short.MAX_VALUE)));

		pack();
	}

	private boolean initializeBrokerManagerURI() {
		String bmu = "";
		while (bmu.equals("")) {
			bmu = JOptionPane.showInputDialog("Insert broker manager URI",
					"10.42.43.1");
			if (bmu == null) {
				return false;
			}
		}
		brokerManagerURI = bmu;
		this.setVisible(true);
		return true;
	}

	public void printGuiMessage(String sourceName, String msg, String topic) {
		panelMessagesLog.getTextAreaMessages().append(
				"[" + topic + "] " + sourceName + ": " + msg + "\n");
		panelMessagesLog.getTextAreaMessages().setCaretPosition(
				panelMessagesLog.getTextAreaMessages().getText().length());
	}

	private void setCenterLocation() {
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		Dimension size = this.getSize();
		int x = (screenSize.width - size.width) / 2;
		int y = (screenSize.height - size.height) / 2;
		this.setLocation(x, y);
	}
}
